package com.s69.appback.controller;

import com.github.pagehelper.PageInfo;
import com.s69.appback.dto.AppInfoDto;
import com.s69.appback.entity.AppInfo;
import com.s69.appback.service.AppInfoService;
import com.s69.appback.vo.ResultVo;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.UUID;

@RestController
@RequestMapping("/appinfo")
public class AppInfoController {
    @Autowired
    AppInfoService appInfoService;


    @DeleteMapping("/{id}")
    public ResultVo delApp(@PathVariable Long id,HttpServletRequest request){
        appInfoService.del(id,request);
        return ResultVo.success("",null);
    }
    @GetMapping("/shenhe")
    public ResultVo onShenHe(Long id,Integer ofType){
        AppInfo appInfo=new AppInfo();
        appInfo.setId(id);
        if (ofType==1){
            //审核
            appInfo.setStatus(2L);
        }else if (ofType==2){
            //驳回
            appInfo.setStatus(3L);
        }
        appInfoService.update(appInfo);
        return ResultVo.success("",null);
    }

    @GetMapping("/{id}")
    public ResultVo appListInfo(@PathVariable Long id){
       AppInfo appInfo= appInfoService.getListById(id);
       return ResultVo.success("",appInfo);
    }

    @PostMapping("/saveOrUpdate")
    public ResultVo saveOrUpdate(AppInfo appInfo, MultipartFile logo,HttpServletRequest request){
        if (logo!=null && !logo.isEmpty()){
            String realPath = request.getServletContext().getRealPath("/upload/appinfo/logo");
            File file=new File(realPath);
            if (!file.exists()){
                file.mkdirs();
            }
            String originalFilename = logo.getOriginalFilename();
            String suffixName = FilenameUtils.getExtension(originalFilename);
            if (logo.getSize()>1024*500){
                throw new RuntimeException("文件大小超过500k");
            } else if (suffixName.equalsIgnoreCase("jpg") ||
                        suffixName.equalsIgnoreCase("jpeg") ||
                        suffixName.equalsIgnoreCase("gif") ||
                        suffixName.equalsIgnoreCase("png") ) {
                String newFilename = UUID.randomUUID().toString().replace("-", "") + "." + suffixName;
                File saveFile=new File(realPath+"/"+newFilename);
                try {
                    logo.transferTo(saveFile);
                    appInfo.setLogopicpath("/upload/appinfo/logo/"+newFilename);
                } catch (IOException e) {
                    e.printStackTrace();
                    return ResultVo.error("文件上传异常！");
                }
            }else {
                return ResultVo.error("文件格式错误！");
            }
        }
        //数据库操作
        String token = request.getHeader("token");
        String[] split = token.split("-");
        if (appInfo.getId()==null){
            //新增
            appInfo.setCreationdate(new Date());
            appInfo.setCreatedby(Long.valueOf(split[2]));
            appInfo.setStatus(1L);
            appInfo.setDownloads(0L);
            appInfo.setDevid(Long.valueOf(split[2]));
            appInfoService.save(appInfo);
        }else {
            appInfo.setModifydate(new Date());
            appInfo.setModifyby(Long.valueOf(split[2]));
            appInfo.setStatus(1L);
            appInfoService.update(appInfo);
        }
        return ResultVo.success("",null);
    }

    @PostMapping("/getPage")
    public ResultVo getPage(@RequestBody AppInfoDto appInfoDto, HttpServletRequest request, @RequestParam(defaultValue = "1")Integer pageNum){
        PageInfo<AppInfo> list=appInfoService.getPage(appInfoDto,request,pageNum);
        return ResultVo.success("",list);
    }

    @GetMapping("/onOff")
    public ResultVo onOff(Long id){
        appInfoService.onOff(id);
        return ResultVo.success("",null);
    }

    @PostMapping("/validate")
    public ResultVo checkApk(@RequestBody AppInfo appInfo){
            Boolean flag=appInfoService.validate(appInfo);
            if (flag==true){
                return ResultVo.success("",null);
            }else {
                return  ResultVo.error("");
            }
    }

}
